package com.apotik.legenda.main;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PageFormat;
import java.util.List;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.PrinterName;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.apotik.legenda.dao.ConfigsPrinterDao;
import com.apotik.legenda.model.ConfigPrinter;
import com.apotik.legenda.util.CalendarUtil;

public class PrintKwitansiMain {

	public static void main(String[] args) {
		
		ConfigsPrinterDao dao = new ConfigsPrinterDao();
		Criterion cr1 = Restrictions.eq("useFor", "KWITANSI_RESEP");
		final List<ConfigPrinter> cfList = dao.loadBy(Order.asc("useIndex"), cr1);
		
		PageFormat pageFormat = new PageFormat();
		Paper paper = new Paper();
		//paper.setSize(width, height);
		
		
		HashAttributeSet attributes = new HashAttributeSet();
        attributes.add(new PrinterName("Microsoft XPS Document Writer", null));
        DocFlavor format = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
        PrintService[] services = PrintServiceLookup.lookupPrintServices(format, attributes);
        System.out.println("service: "+services.length);
        if (services.length > 0) {
        	 Doc doc = new SimpleDoc(new Printable() {
				@Override
				public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
					if (pageIndex==0) {
						Graphics2D g2d = (Graphics2D)g;
				        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
				        //int x = (int) pageFormat.getImageableX();
				        //int y = (int) pageFormat.getImageableY();
						
				        for(int i=0;i<cfList.size();i++){
				        	ConfigPrinter cp = cfList.get(i);
				        	
				        	String strName = cp.getStringValue();
				        	if(cp.getUseIndex()==5){
				        		strName = cp.getStringValue()+""+i+"/"+i;
				        	}else if(cp.getUseIndex()==6){
				        		strName = cp.getStringValue()+" "+CalendarUtil.calIndonesia_DD_MMM_YYYY();
				        	}
				        	
				        	g.setFont(new Font(cp.getFontName(), cp.getFontStyle(), cp.getFontSize()));
							g.drawString(strName, cp.getStringX(), cp.getStringY());
				        }
						
						return PAGE_EXISTS;
					}else{
						return NO_SUCH_PAGE;
					}
				}
        	 }, format, null);
        	 try {
        		 PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
                 services[0].createPrintJob().print(doc, aset);
             } catch (PrintException ex) {
                 ex.printStackTrace();
             }
        }
	}
}
